#include "stdafx.h"
#include "NaviRes.h"

#define RES_FILE	CString(L"NavigateRes.dll")

#define INSERT_BMP_RES(_map, _mod, _id)\
	_map.insert(std::make_pair(_id, ::LoadBitmap(_mod, MAKEINTRESOURCE(_id))))

#define INSERT_CURSOR_RES(_map, _mod, _id)\
	_map.insert(std::make_pair(_id, ::LoadCursor(_mod, MAKEINTRESOURCE(_id))))

void NaviRes::InitRes()
{
	CString sPath = NavTolFun::GetModulePath();
	m_hRes = ::LoadLibrary(sPath += RES_FILE);
	if (!m_hRes)
	{
		CString err(L"");
		err.Format(L"Load file of %s failed!", sPath);
		AfxMessageBox(err);
		exit(0);
	}

	INSERT_BMP_RES(m_Bmp, m_hRes, IDB_BMP_UNKNOW);

	INSERT_BMP_RES(m_Bmp, m_hRes, IDB_BMP_DENG_LING);
	INSERT_BMP_RES(m_Bmp, m_hRes, IDB_BMP_DENG_MEI);
	INSERT_BMP_RES(m_Bmp, m_hRes, IDB_BMP_DIAO_JIA);
	INSERT_BMP_RES(m_Bmp, m_hRes, IDB_BMP_FA_LAN);
	INSERT_BMP_RES(m_Bmp, m_hRes, IDB_BMP_FENG_GUAN);
	INSERT_BMP_RES(m_Bmp, m_hRes, IDB_BMP_KAI_SHUO);
	INSERT_BMP_RES(m_Bmp, m_hRes, IDB_BMP_MEN_CHUANG);
	INSERT_BMP_RES(m_Bmp, m_hRes, IDB_BMP_NUAN_TONG_GUAN_JIAN);
	INSERT_BMP_RES(m_Bmp, m_hRes, IDB_BMP_NUAN_TONG_SHE_BEI);
	INSERT_BMP_RES(m_Bmp, m_hRes, IDB_BMP_QIANG);
	INSERT_BMP_RES(m_Bmp, m_hRes, IDB_BMP_QIAO_JIA);
	INSERT_BMP_RES(m_Bmp, m_hRes, IDB_BMP_QIAO_JIA_SHAN_TONG);
	INSERT_BMP_RES(m_Bmp, m_hRes, IDB_BMP_QIAO_JIA_SHI_TONG);
	INSERT_BMP_RES(m_Bmp, m_hRes, IDB_BMP_QIAO_JIA_YI_ZHI_WAN);
	INSERT_BMP_RES(m_Bmp, m_hRes, IDB_BMP_QIAO_JIA_ZHI_JIAO);
	INSERT_BMP_RES(m_Bmp, m_hRes, IDB_BMP_SHAN_RE_QI);
	INSERT_BMP_RES(m_Bmp, m_hRes, IDB_BMP_SHUO);
	INSERT_BMP_RES(m_Bmp, m_hRes, IDB_BMP_ZI_JIA);
	INSERT_BMP_RES(m_Bmp, m_hRes, IDB_BMP_ZU_ZI);

	INSERT_BMP_RES(m_Bmp, m_hRes, IDB_BMP_SHUI_GUAN);
	INSERT_BMP_RES(m_Bmp, m_hRes, IDB_BMP_SHUANG_XIAN_SHUI_GUAN);
	INSERT_BMP_RES(m_Bmp, m_hRes, IDB_BMP_FA_MEN_FA_JIAN);
	INSERT_BMP_RES(m_Bmp, m_hRes, IDB_BMP_GEI_PAI_SHUI_FU_JIAN);
	INSERT_BMP_RES(m_Bmp, m_hRes, IDB_BMP_BENG);

	INSERT_BMP_RES(m_Bmp, m_hRes, IDB_TBAR_MAINFRAME);
	INSERT_BMP_RES(m_Bmp, m_hRes, IDB_TBAR_PROPERTIESWND);
	INSERT_BMP_RES(m_Bmp, m_hRes, IDB_TBAR_VIEW3D);
	INSERT_BMP_RES(m_Bmp, m_hRes, IDB_TBAR_ZOOM);

	INSERT_CURSOR_RES(m_Cursor, m_hRes, IDC_CUR_DYNAMICOBSERVE);
	INSERT_CURSOR_RES(m_Cursor, m_hRes, IDC_CUR_DYNAMICOBSERVE_X);
	INSERT_CURSOR_RES(m_Cursor, m_hRes, IDC_CUR_DYNAMICOBSERVE_Y);
	INSERT_CURSOR_RES(m_Cursor, m_hRes, IDC_CUR_DYNAMICOBSERVE_Z);
	INSERT_CURSOR_RES(m_Cursor, m_hRes, IDC_CUR_MANUALNAVIGATE);
	INSERT_CURSOR_RES(m_Cursor, m_hRes, IDC_CUR_REALTIMEZOOM);
	INSERT_CURSOR_RES(m_Cursor, m_hRes, IDC_CUR_TRANSLATE);
	INSERT_CURSOR_RES(m_Cursor, m_hRes, IDC_CUR_WINDOWZOOM);
}

NaviRes::~NaviRes()
{
	for (mapBmp::iterator it = m_Bmp.begin(); it != m_Bmp.end(); ++it)
	{
		if (it->second)
			::DeleteObject(it->second);
	}
	for (mapCursor::iterator it = m_Cursor.begin(); it != m_Cursor.end(); ++it)
	{
		if (it->second)
			::DestroyCursor(it->second);
	}
	if (m_hRes) ::FreeLibrary(m_hRes);
}

const HBITMAP* NaviRes::FindRes( UINT id, const mapBmp & res ) const
{
	mapBmp::const_iterator it = res.end();
	if ((it = res.find(id)) != res.end())
		return &it->second;
	return NULL;
}

const HCURSOR* NaviRes::FindRes( UINT id, const mapCursor & res ) const
{
	mapCursor::const_iterator it = res.end();
	if ((it = res.find(id)) != res.end())
		return &it->second;
	return NULL;
}